#include "epollE.h"

using namespace easyWebServer;

Epoll::Epoll()
{

}

Epoll::~Epoll()
{
    if(m_event != nullptr)
    {
        delete m_event;
        m_event = nullptr;
    }
    if(m_fd != -1)
    {
        close(m_fd);
        m_fd = -1;
    }
}

void Epoll::setEpollEventSize(int size)
{
    m_event = new struct epoll_event[size];
    m_size = size;
}

void Epoll::createEpollfd()
{
    m_fd = epoll_create(m_size);
}

void Epoll::addEvent(int fd, uint32_t event)
{
    if(m_fd == -1 || fd == -1) return;
    struct epoll_event ev;
    ev.data.fd = fd;
    ev.events = EPOLLET | event;
    epoll_ctl(m_fd, EPOLL_CTL_ADD, fd, &ev);
}

void Epoll::addEvent(int fd, uint32_t event, unique_ptr<Task> & task)
{
    if(m_fd == -1 || fd == -1) return;
    struct epoll_event ev;
    ev.data.fd = fd;
    ev.data.ptr = task.release();
    ev.events = EPOLLET | event;
    epoll_ctl(m_fd, EPOLL_CTL_ADD, fd, &ev);
}


void Epoll::delEvent(int fd)
{
    epoll_ctl(m_fd, EPOLL_CTL_DEL, fd, NULL);
}

int Epoll::epollWait(int time)
{
    if(m_fd == -1 || m_event == nullptr) return -1;
    return epoll_wait(m_fd, m_event, m_size, time);
}

struct epoll_event * Epoll::getEventList()
{
    return m_event;
}



